package com.cjh.springcloud.controller;

import com.cjh.springcloud.pojo.Emp;
import com.cjh.springcloud.service.EmpService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author cjh
 * @Date 2020/12/3 17:27
 * @Version 1.0
 * 服务熔断
 */
@RestController
@RequestMapping("/provider")
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping("/queryOne/{empId}")
    @HystrixCommand(fallbackMethod = "hystrixGet")// 代码出现异常时，跳到hystrixGet()
    public Emp get(@PathVariable("empId") Integer empId) {
        Emp emp = empService.queryById(empId);
        if (ObjectUtils.isEmpty(emp)) {
            throw new RuntimeException("empId=>" + empId + "，不存在这个员工，或者无法找到。");
        }
        return emp;
    }

    // 服务熔断方法
    public Emp hystrixGet(@PathVariable("empId") Integer empId) {

        return new Emp().setEmpId(empId)
                .setEmpName("empId=>" + empId + "，没有对应的信息，null--@Hystrix。")
                .setDataSource("no data in MySql。");
    }

}
